package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Studentquestion;
import com.ruoyi.system.domain.Volunteer;
import com.ruoyi.system.service.VolunteerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/system/volunteer")
public class VolunteerController extends BaseController {

    @Autowired
    private VolunteerService volunteerService;

    @PreAuthorize("@ss.hasPermi('system:volunteer:list')")
    @GetMapping("/list")
    public TableDataInfo list(Volunteer volunteer)
    {
        System.out.println(volunteer);
        startPage();
        List<Volunteer> list = volunteerService.selectVolunteerList(volunteer);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:volunteer:query')")
    @GetMapping(value = "/{vId}")
    public AjaxResult getInfo(@PathVariable Long vId)
    {
        return AjaxResult.success(volunteerService.selectVolunteerById(vId));
    }

    @PreAuthorize("@ss.hasPermi('system:volunteer:add')")
    @Log(title = "志愿管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Volunteer volunteer)
    {
        volunteer.setCreateBy(getUsername());
        return toAjax(volunteerService.insertVolunteer(volunteer));
    }

    @PreAuthorize("@ss.hasPermi('system:volunteer:edit')")
    @Log(title = "志愿管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Volunteer volunteer)
    {
        volunteer.setUpdateBy(getUsername());
        return toAjax(volunteerService.updateVolunteer(volunteer));
    }

    @PreAuthorize("@ss.hasPermi('system:volunteer:remove')")
    @Log(title = "志愿管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{vIds}")
    public AjaxResult remove(@PathVariable Long[] vIds)
    {
        return toAjax(volunteerService.deleteVolunteerByIds(vIds));
    }

    @PreAuthorize("@ss.hasPermi('system:volunteer:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Volunteer volunteer)
    {
        List<Volunteer> list = volunteerService.selectVolunteerList(volunteer);
        ExcelUtil<Volunteer> util = new ExcelUtil<Volunteer>(Volunteer.class);
        util.exportExcel(response, list, "【】数据");
    }
}
